package com.etc.controller;

import com.etc.pojo.Friend;
import com.etc.pojo.User;
import com.etc.service.IUserService;
import com.etc.service.IfriendService;
import com.etc.service.impl.IfriendServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: RookieSocialNetworking
 * @description: 好友控制器
 * @author: Mr.单
 * @create: 2020-08-18 13:28
 **/
@Controller
@RequestMapping("FriendController")
public class FriendController {
    @Autowired
    IfriendService ifriendService;
    @Autowired
    IUserService userService;
    @ResponseBody
    @RequestMapping("queryfirend")
    public PageInfo<Friend> queryfirend(HttpSession session, String pn, String ps){
        Integer pageNum = 1;
        Integer pageSize = 3;
        if(pn!=null && !pn.trim().equals("")){
            pageNum = Integer.parseInt(pn);
        }
        if(ps!=null && !ps.trim().equals("")){
            pageSize = Integer.parseInt(ps);
        }
        User user =(User) session.getAttribute("user");
        PageInfo<Friend> pageInfo =  ifriendService.getFriendByUserId(user.getUserId(),pageNum,pageSize);

        return pageInfo;
    }

    @ResponseBody
    @RequestMapping("queryMyfirendByKey")
    public PageInfo<Friend> queryMyfirendByKey(HttpSession session, String pn, String ps,String kw){
        Integer pageNum = 1;
        Integer pageSize = 3;
        String keyword = "";
        if(pn!=null && !pn.trim().equals("")){
            pageNum = Integer.parseInt(pn);
        }
        if(ps!=null && !ps.trim().equals("")){
            pageSize = Integer.parseInt(ps);
        }
        if(kw!=null){
            keyword = kw;
        }
        User user = new User();
        user.setUserName(kw);
        user.setUserTel(kw);;
        PageInfo<Friend> pageInfo =  ifriendService.queryMyFriendByNameandTel(user,pageNum,pageSize);
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping("queryfirendByKey")
    public PageInfo<User> queryfirendByKey(HttpSession session, String pn, String ps,String kw){
        Integer pageNum = 1;
        Integer pageSize = 3;
        String keyword = "";
        if(pn!=null && !pn.trim().equals("")){
            pageNum = Integer.parseInt(pn);
        }
        if(ps!=null && !ps.trim().equals("")){
            pageSize = Integer.parseInt(ps);
        }if(kw!=null){
            keyword = kw;
        }
        User user = new User();
        user.setUserName(kw);
        user.setUserTel(kw);;

        User user1 =(User) session.getAttribute("user");
        PageInfo<Friend> friendList = ifriendService.getFriendByUserId(user1.getUserId(), 1, 0);
        List<Integer> fids = new ArrayList<>();
        for (Friend friend : friendList.getList()) {
            fids.add(friend.getFriend().getUserId());
        }
        fids.add(user1.getUserId());
        PageInfo<User> pageInfo =  userService.queryNotAddUserByNameOrTel(fids,user,pageNum,pageSize);
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping("firendtuijie")
    public PageInfo<User> firendtuijie(HttpSession session, String pn, String ps){
        Integer pageNum = 1;
        Integer pageSize = 3;
        String keyword = "";
        if(pn!=null && !pn.trim().equals("")){
            pageNum = Integer.parseInt(pn);
        }
        if(ps!=null && !ps.trim().equals("")){
            pageSize = Integer.parseInt(ps);
        }

        User user =(User) session.getAttribute("user");
        PageInfo<Friend> friendList = ifriendService.getFriendByUserId(user.getUserId(), 1, 0);
        List<Integer> fids = new ArrayList<>();
        for (Friend friend : friendList.getList()) {
            fids.add(friend.getFriend().getUserId());
        }
        fids.add(user.getUserId());
        PageInfo<User> pageInfo =  userService.queryNotAddUser(fids,pageNum,pageSize);
        return pageInfo;
    }
    @RequestMapping("addfriend")
    @ResponseBody
    public boolean addfriend(String friendId,String userId){
        Integer fid =Integer.parseInt(friendId);
        Integer uid = Integer.parseInt(userId);
        return ifriendService.addFriendok(fid,uid,0);
    }


    @ResponseBody
    @RequestMapping("friendCount")
    public Integer friendCount(HttpSession session){
        User user =(User) session.getAttribute("user");
        System.out.println("userId:" + user.getUserId());
        Integer count =  ifriendService.getFriendCount(user.getUserId());
        return count;
    }

    @ResponseBody
    @RequestMapping("queryFirendApply")
    public PageInfo<Friend> queryFirendApply(HttpSession session, String pn, String ps){
        Integer pageNum = 1;
        Integer pageSize = 3;
        if(pn!=null && !pn.trim().equals("")){
            pageNum = Integer.parseInt(pn);
        }
        if(ps!=null && !ps.trim().equals("")){
            pageSize = Integer.parseInt(ps);
        }
        User user =(User) session.getAttribute("user");
        PageInfo<Friend> pageInfo =  ifriendService.getFriendApply(user.getUserId(),pageNum,pageSize);
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping("agreeFriend")
    public boolean agreeFriend(HttpSession session,Integer fid,Integer ID){
        User user = (User) session.getAttribute("user");
        Friend friend = new Friend();
        friend.setId(ID);
        friend.setFriendStatus(1);
        boolean b =  ifriendService.updateFriend(friend);
        ifriendService.addFriendok(fid,user.getUserId(),1);
        return b;
    }

    @ResponseBody
    @RequestMapping("jujueFriend")
    public boolean jujueFriend(Integer fid){
        Friend friend = new Friend();
        friend.setId(fid);
        friend.setFriendStatus(2);
        boolean b =  ifriendService.updateFriend(friend);
        return b;
    }
}
